// pages/personal/percenter.js
var util = require('../../utils/util');
var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    barimages: ['../../images/bnav4n.png', '../../images/bnav2n.png', '../../images/bnav3h.png'],
    currentTab:0,
    datanum:1, //0表示没有数据,1表示 有数据
    userInfo: { headImg:'../../images/btnadd.png',nickName:'菠萝君健身',id:''},
    imgPath:'../../images/zhuzi.png',
    condition:{},
    BMI:"0.0",
    // weightList:[],
    // weightNow:'-',
    // weightDiff:'0.0',
    // otherList:[],
    // otherNow: '-',
    // otherDiff: '0.0',
    nowType:2
  },

  onLoad:function(){
    that = this;
    util.sendRequest('WxGetUserInfo',{},function(resp){
      
      if (resp.code==0){
        that.setData({ userInfo: resp.result});
      }else{
        util.wxModalAlert({content:resp.text});
      }
      
    },'加载中...',function(){

    });

    util.sendRequest('WXGetConditionLog', { type:1}, function (resp) {

      var condition = that.data.condition;
      condition[1] = { list: resp.result.list, nowData: (parseFloat(resp.result.nowData)*2) || '0.0', diff: resp.result.diff || '0.0' };
      that.setData({ condition: condition, BMI: that.getBMI(that.data.userInfo.height, resp.result.nowData)});
      that.drawWeightChart('wCanvas',resp.result.list);
    }, '加载中...', function () {

    });

    util.sendRequest('WXGetConditionLog', { type: 2 }, function (resp) {

      var condition = that.data.condition;
      condition[2] = { list: resp.result.list, nowData: resp.result.nowData || '0.0', diff: resp.result.diff || '0.0' };
      that.setData({ condition: condition });
      if (resp.result.list.length > 0) {
        that.setData({ datanum: 1 });
        that.drawWeightChart('hCanvas', resp.result.list);
      } else {
        that.setData({ datanum: 0 });
      }
       
    }, '加载中...', function () {

    });

  },
  getBMI:function(a,b){
    // BMI = 體重(公斤) / 身高2(公尺2)
    if (a == undefined || b == undefined) return "0.0";
    var bmi = b / ((a / 100) * (a / 100));
    return Math.floor(bmi * 100) / 100;
  },
  toSetup:function(){
    wx.navigateTo({
      url: '/pages/personal/perinfo',
    })
  },
  drawWeightChart:function(canvasid,list){
    const  context = wx.createCanvasContext(canvasid);

    var minSize = 7;
    var size = minSize;
    var windowWidth = 0;
    var width = 0;
    var height = 150;
    var  HArr = [];
    var wArr = [];
  //计算list 每格宽度
    if(list instanceof Array){
       size= list.length;
        if (size <= minSize){
          size = minSize;
        }
        // size = size+1;
        //格式化 横纵坐标数值
        var minMax = that.getMinMax(list);
        var perH = 0;
        if(list.length==1){
          HArr = [(minMax.min - 5) <= 0 ? 0 : (minMax.min - 5), minMax.min, minMax.min + 5, minMax.min + 10];
        } else if(list.length == 2){
          perH = Math.floor((minMax.max - minMax.min));
          HArr = [(minMax.min - perH) <= 0 ? 0 : (minMax.min - perH), minMax.min, minMax.min + perH, minMax.min + perH * 2 ];
        }else{
          perH = Math.floor((minMax.max - minMax.min) / 2);
          HArr = [minMax.min - perH, minMax.min, minMax.min + perH, minMax.min + perH * 2];
        }

        
        for(var i =0 ; i <size ; i ++){
          wArr.push((i+1));
        }
        wx.getSystemInfo({
          success: function (res) {
            windowWidth = res.windowWidth;
          }
        })
        //宽度每格px
        var perW = windowWidth/(size+3);
        //高度每格
        perH = 30;
        
        //划线
        context.moveTo(20,120);
        context.lineTo(windowWidth-60,120);
        context.stroke();

        context.setStrokeStyle("#F5F5F5");
        context.moveTo(20, 90);
        context.lineTo(windowWidth - 60, 90);
        context.moveTo(20, 60);
        context.lineTo(windowWidth - 60, 60);
        context.moveTo(20, 30);
        context.lineTo(windowWidth - 60, 30);
        // context.stroke();
        // context.draw();

        var fontSize = 10;
        context.setFontSize(fontSize);
         
        for (var i = 0; i < wArr.length ; i ++){
          context.fillText(wArr[i], (20+perW*(i+1)),135);
        }
        for (var i = 0; i < HArr.length; i++) {
          context.fillText(HArr[i], 0, 120-perH*i);
        }
        context.stroke();
        // context.draw();
        var ratioH = 90/(HArr[HArr.length-1] - HArr[0]);


        for (var i = 0; i < list.length; i++) {
          var v = list[i].val;
          var diff = v - HArr[0];
          context.drawImage(that.data.imgPath, (20 + perW * (i + 1)), 120 - (diff * ratioH), 10, diff * ratioH-1);
        }
      context.draw();

    }else{
      return;
    }

    
  },
  getMinMax: function (list){
      var min = 0;
      var max = 0;
      for(var i = 0 ; i < list.length ; i++){
        if(i==0){
          min = list[i].val;
          max = list[i].val;
        }
        if (list[i].val<min){
          min = list[i].val;
        }
        if (list[i].val > max){
          max = list[i].val ;
        }
      }
      return {min:min,max:max};
  },

  addW:function(){
    wx.redirectTo({
      url: '/pages/function/weight',
    })
  },
  addO:function(){
    wx.setStorageSync('conditionType', that.data.nowType);
    wx.redirectTo({
      url: '/pages/function/size',
    })
  },
  /** 滑动切换tab */
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current});
  },
  /** 点击tab切换 */
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
    if (!that.data.condition[(parseInt(e.target.dataset.current) + 2)]){
      util.sendRequest('WXGetConditionLog', { type: (parseInt(e.target.dataset.current) + 2) }, function (resp) {
        var condition = that.data.condition;
        condition[(parseInt(e.target.dataset.current) + 2)] = { list: resp.result.list, nowData: resp.result.nowData || '0.0', diff: resp.result.diff || '0.0' };
        that.setData({ condition: condition });
        if(resp.result.list.length>0){
          that.setData({ datanum: 1 });
          that.drawWeightChart('hCanvas', resp.result.list);
        }else{
          that.setData({ datanum: 0 });
        }
        
      }, '加载中...', function () {

      });
    }else{
      var condition = that.data.condition[(parseInt(e.target.dataset.current) + 2)];
   
      if (condition.list.length > 0) {
        that.setData({ datanum: 1 });
        that.drawWeightChart('hCanvas', condition.list);
      } else {
        that.setData({ datanum: 0 });
      }
    }
    
  },
  cf:function(){
    wx.redirectTo({
      url: '/pages/index/index',
    })
  },
  lx:function(){
    wx.redirectTo({
      url: '/pages/personal/perplan2',
    })
  },
  onShareAppMessage: function () {
   return util.getShareMsg();
  }
})